package com.link_with_health.config;

import com.alibaba.fastjson.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
public class MsgConfig<T> {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());
    private int code;
    private String message;
    private T date;

    public MsgConfig(){}

    public MsgConfig(int code, String message, T date) {
        this.code = code;
        this.message = message;
        this.date = date;
    }

    /**
     * 获取数据成功
     * @param date
     * @return
     */
    public MsgConfig sucMsg(T date,String msg){
        this.code = 0;
        this.date = date;
        this.message= msg;
        return this;
    }

    /**
     * 获取数据失败
     * @return 500
     */
    public MsgConfig fMsg(String errorMsg){
        this.code = 500;
        this.date = null;
        this.message= errorMsg;
        return this;
    }

    public static JSONObject resultMsg(MsgConfig config){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",config.getCode());
        jsonObject.put("message",config.getMessage());
        jsonObject.put("data",config.getDate());

        return jsonObject;
    }

    public static String resultMsgStr(MsgConfig config){
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("code",config.getCode());
        jsonObject.put("message",config.getMessage());
        jsonObject.put("data",config.getDate());

        return jsonObject.toJSONString();
    }


    //权限不足

    public MsgConfig authMsg(){
        return new MsgConfig(400," ",0);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getDate() {
        return date;
    }

    public void setDate(T date) {
        this.date = date;
    }
}
